package com.rmondjone.face

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.hlb.group.module.redpackage.adapter.HistoryTabAdapter
import com.rmondjone.androidcamerademo.MainActivity
import com.rmondjone.androidcamerademo.R
import com.rmondjone.androidcamerademo.bean.CameraType
import com.rmondjone.androidcamerademo.dp
import com.rmondjone.camera.BaseActivity
import com.rmondjone.face.ViewModel.FaceViewModel
import com.rmondjone.utils.StatusBarUtil
import com.rmondjone.utils.setClickListener
import kotlinx.android.synthetic.main.activity_face_consulation.*
import kotlinx.coroutines.*

/**
 * copyright：
 * @author：anthui creation date： .
 * @since: 1.0.0
 * @describe:
 */
class FaceConsultationActivity : BaseActivity() {

    var isFirst = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        StatusBarUtil.setStatusBarDarkFontMode(this)
        setContentView(R.layout.activity_face_consulation)

        iv_back.setClickListener {
            MainActivity.isClick = false
            MainActivity.startActivity(this, intent.getIntExtra("type", CameraType.CAMERA_TYPE_FMIAN_ZHEN))
            finish()
        }

        tv_biaoxian.setClickListener {

            if (isFirst) {
                return@setClickListener
            }

            isFirst = true
            view_page.isSelected = false

            tv_biaoxian.layoutParams.let {
                it as ConstraintLayout.LayoutParams
                it.marginStart = 0
                it.marginEnd = 0
                it.height = 50.dp.toInt()
                tv_biaoxian.layoutParams = it
            }
            tv_jianyi.layoutParams.let {
                it as ConstraintLayout.LayoutParams
                it.marginEnd = 30.dp.toInt()
                it.marginStart = 30.dp.toInt()
                it.height = 40.dp.toInt()

                tv_jianyi.layoutParams = it
            }


            tv_biaoxian.isSelected = true
            tv_jianyi.isSelected = false
            view_page.setCurrentItem(0, false)

            view_line_biaoxian.visibility = View.VISIBLE
            view_line_jianyi.visibility = View.INVISIBLE
        }

        tv_jianyi.setClickListener {

            if (!isFirst) {
                return@setClickListener
            }


            view_page.isSelected = true


            tv_biaoxian.layoutParams.let {
                it as ConstraintLayout.LayoutParams
                it.height = 40.dp.toInt()

                it.marginStart = 30.dp.toInt()
                it.marginEnd = 30.dp.toInt()
                tv_biaoxian.layoutParams = it
            }
            tv_jianyi.layoutParams.let {
                it as ConstraintLayout.LayoutParams
                it.height = 50.dp.toInt()

                it.marginEnd = 0
                it.marginStart = 0
                tv_jianyi.layoutParams = it
            }

            tv_jianyi.isSelected = true
            tv_biaoxian.isSelected = false
            isFirst = false

            view_page.setCurrentItem(1, false)

            view_line_biaoxian.visibility = View.INVISIBLE
            view_line_jianyi.visibility = View.VISIBLE
        }
        tv_biaoxian.isSelected = true

        initData()
    }


    private fun startLoad() {

        layout_load.visibility = View.VISIBLE
//        Glide.with(this).load(R.drawable.loading).into(iv_load)
//
//        正在五官识别...
//        正在体质解析...
//        正在生成您的分析报告
        GlobalScope.launch {
            withContext(Dispatchers.Main) {

                if (intent.getIntExtra("type", CameraType.CAMERA_TYPE_FMIAN_ZHEN) == CameraType.CAMERA_TYPE_FMIAN_ZHEN) {
                    tv_load.text = "正在脸部识别..."
                } else {
                    tv_load.text = "正在舌部识别..."

                }
            }
            delay(1000)
            withContext(Dispatchers.Main) {
                tv_load.text = "正在体质解析..."
            }
            delay(1000)
            withContext(Dispatchers.Main)
            {
                tv_load.text = "正在生成您的分析报告..."
            }
            delay(1000)

            if (isLoadErr) {
                return@launch
            }

            if (isLoadFinish) {
                withContext(Dispatchers.Main) {
                    layout_load.visibility = View.GONE
                }
            }
            isLoadFinish = true

        }
    }

    var isLoadFinish = false

    var isLoadErr = false
    private fun initData() {
        startLoad()
        val get = ViewModelProviders.of(this).get(FaceViewModel::class.java)

        get.isLoadErr.observe(this, Observer {
            isLoadErr = true

            TipsDialog().apply {
                tips = it
                click = {
                    MainActivity.isClick = false
                    MainActivity.startActivity(this@FaceConsultationActivity, intent.getIntExtra("type", CameraType.CAMERA_TYPE_FMIAN_ZHEN))
                    finish()
                }
                show(supportFragmentManager)
            }

        })

        get.isLoadFinish.observe(this, Observer {
            if (isLoadFinish) {
                layout_load.visibility = View.GONE
            }

            isLoadFinish = true

            view_page.visibility = View.VISIBLE
            rg_tag.visibility = View.VISIBLE
            layout_line.visibility = View.VISIBLE
            initRecyclerView()

        })

        get.getData(intent.getStringExtra("path"), intent.getIntExtra("type", CameraType.CAMERA_TYPE_FMIAN_ZHEN))


    }

    fun initRecyclerView() {


        val fragments = arrayListOf<Fragment>()

        fragments.add(SymptomFragment())
        fragments.add(SuggestFragment())
        view_page.adapter = HistoryTabAdapter(supportFragmentManager, fragments)
    }

    override fun onBackPressed() {
//        super.onBackPressed()
        MainActivity.isClick = false
        MainActivity.startActivity(this, intent.getIntExtra("type", CameraType.CAMERA_TYPE_FMIAN_ZHEN))
        finish()
    }
}